<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/6/6
 * Time: 21:19
 */
namespace app\api\validate;

use app\admin\model\User;
use app\common\model\Goods;
use app\common\model\GoodsCategory;

class Order extends Base
{
    protected $rule = [
        'id' => 'integer|min:1|goodsExists|levelVerify',
    ];

    protected $message = [
        'id.goodsExists' => '商品不存在',
        'id.levelVerify' => '购买产品等级与当前会员等级不符',
    ];

    /**
     * 商品存在
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * @throws \think\exception\DbException
     */
    protected function goodsExists($value, $rule, $data)
    {
        if(!Goods::get(['id'=>$value])) return false;
        return true;
    }

    /**
     * 等级校验
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * @throws \think\exception\DbException
     */
    protected function levelVerify($value, $rule, $data)
    {
        $goods = Goods::get(['id'=>$value]);
        $user = User::get(['id'=>$data['user_id']]);
        if($goods->level<$user->level && $goods->goods_category_id==1 && ($user->level!=0 && $user->level_duedate>time()))
        {
            return false;
        }
        return true;
    }

}